package com.zhang.controller;

import com.github.pagehelper.PageInfo;
import com.zhang.entity.Category;
import com.zhang.entity.Product;
import com.zhang.service.CategoryService;
import com.zhang.service.ProductService;
import com.zhang.service.impl.CategoryServiceImpl;
import com.zhang.service.impl.ProductServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/main")
public class MainController {
    private CategoryService cs = new CategoryServiceImpl();
    private ProductService ps = new ProductServiceImpl();

    @RequestMapping("/showAll")
    public String showAll(Model model){
        List<Category> cateList = cs.selectAll();
        List<Product> list1 = ps.selectCon("salenum asc", 2);
        List<Product> list2 = ps.selectCon("salenum desc", 8);
        List<Product> list3 = ps.selectCon("groundingDate desc", 8);
        List<Product> list4 = ps.selectCon("groundingDate,salenum desc", 8);

        model.addAttribute("cateList",cateList);
        model.addAttribute("list1",list1);
        model.addAttribute("list2",list2);
        model.addAttribute("list3",list3);
        model.addAttribute("list4",list4);

        return "forward:/main/main.jsp";
    }

    @RequestMapping("/selectById")
    public String selectById(Model model,int id){
        Product product = ps.selectById(id);
        model.addAttribute("pro",product);

        return "forward:/main/product.jsp";
    }

    @RequestMapping("/selectPage")
        public String selectPage(@RequestParam(defaultValue = "-1") int fcid,
        @RequestParam(defaultValue = "-1") int scid,
        @RequestParam(defaultValue = "1")int pageNum,
        @RequestParam(defaultValue = "5")int pageSize,
        Model model,int cid,String cname){
            PageInfo<Product> pi = ps.selectBooksByPage(fcid,scid, pageNum, pageSize);
            model.addAttribute("pi",pi);
            model.addAttribute("fcid",fcid);
            model.addAttribute("scid",scid);

            model.addAttribute("cname",cname);

            Category category = cs.selectById(cid);
            model.addAttribute("category",category);

            List<Category> categories = cs.selectNum(cid);
            model.addAttribute("categories",categories);
            model.addAttribute("cid",cid);
        return "forward:/main/book_list.jsp";
    }

}
